﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using AnguaRPGEngine.Library.Systems;
using System.Drawing.Drawing2D;

namespace AnguaRPGEngine.Library.Dice
{
    public class D8 : IDie
    {
        #region ctor
        public D8()
        {
        }
       
        #endregion
        #region IDie Members
        #region Image
        public System.Drawing.Bitmap Image
        {
            get
            {
                Font frontFirstDigitFont = new Font(FontFamily.GenericSansSerif, 20f);
                Font frontSecondDigitFont = new Font(FontFamily.GenericSansSerif, 16f);
                Font frontThirdDigitFont = new Font(FontFamily.GenericSansSerif, 14f);
                Font sideFont = new Font(FontFamily.GenericSansSerif, 8f);
                Bitmap i = ImageLibrary.Instance.GetImageFromLibrary(string.Format("Dice.{0}.D{1}", DieRoller.Instance.DiceCollection.CurrentSet.PathName, Sides, _Value));
                using (Graphics g = Graphics.FromImage(i))
                {
                    string onesSpot = string.Format("{0}", Value);
                    g.DrawString(onesSpot, frontFirstDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(18, 18));

                    if(Value == 6)
                    {
                        g.DrawLine(new Pen(new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), 1f), new Point(25,46), new Point(34,46));
                    }
                    if (SelectSideToTheLeft(onesSpot) == "6")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(1,20), new Point(6,28));
                    }
                    if (SelectSideToTheRight(onesSpot) == "6")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(54, 30), new Point(58, 20));
                    }
                    if (SelectSideBelow(onesSpot) == "6")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(27,52), new Point(32,52));
                    }

                    Matrix matrix = new Matrix();
                    matrix.Rotate(55);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheLeft(onesSpot), frontThirdDigitFont, new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(14, -8));
                    g.ResetTransform();
                    matrix.Rotate(-110);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheRight(onesSpot), frontThirdDigitFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(3, 42));
                    
                    matrix.Reset();
                    matrix.RotateAt(180, new PointF(i.Width/2, i.Height/2));
                    g.Transform = matrix;
                    g.DrawString(SelectSideBelow(onesSpot), sideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(23, 0));
                }
                return i;
            }
        }
        #endregion
        #region Sides
        public int Sides
        {
            get
            {
                return 8;
            }
        }
        #endregion
        #region Value
        private int _Value = 1;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        #endregion
        #region Roll
        public void Roll()
        {
            _Value = Randomizer.Instance.NextInt(1, Sides);
        }
        #endregion
        #endregion
        private string SelectSideToTheLeft(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "5";
                case 2:
                    return "4";
                case 3:
                    return "1";
                case 4:
                    return "6";
                case 5:
                    return "7";
                case 6:
                    return "8";
                case 7:
                    return "3";
            }
            return "2"; // 8
        }
        private string SelectSideBelow(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "6";
                case 2:
                    return "7";
                case 3:
                    return "4";
                case 4:
                    return "3";
                case 5:
                    return "8";
                case 6:
                    return "1";
                case 7:
                    return "2";
            }
            return "5"; // 8
        }
        private string SelectSideToTheRight(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "3";
                case 2:
                    return "8";
                case 3:
                    return "7";
                case 4:
                    return "2";
                case 5:
                    return "1";
                case 6:
                    return "4";
                case 7:
                    return "5";
            }
            return "6"; // 8
        }
    }
}
